﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;

namespace Bewise.Sharepoint.WebPartator
{
    class WebType
    {
        /// <summary>
        /// Returns the list of UserControl types embedded in the assembly passed in parameter
        /// </summary>
        /// <param name="asm">The assembly to introspect</param>
        /// <returns>The list of UserControl</returns>
        public static List<TypeDefinition> GetUserControls(AssemblyDefinition asm)
        {
            List<TypeDefinition> userControls = (from TypeDefinition t in asm.MainModule.Types
                                                 where t.BaseType != null && t.BaseType.Name == "UserControl"
                                                 select t).ToList();

            List<TypeDefinition> webControls = new List<TypeDefinition>();

            foreach (TypeDefinition typeDef in userControls)
            {
                List<TypeDefinition> u = (from TypeDefinition t in asm.MainModule.Types
                                          where t.BaseType != null && t.BaseType.Name == typeDef.Name
                                          select t).ToList();
                foreach (TypeDefinition t in u)
                {
                    webControls.Add(t);
                }
            }
            return webControls;
        }
    }
}
